home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Nebula 1
/
Nebula One.iso
/
Internet
/
WWW
/
gform.1.1
/
util.c
< prev
Wrap
C/C++ Source or Header
|
1995-09-24
|
3KB
|
197 lines
#include <stdio.h>
#include <sys/param.h>
#include <string.h>
#include <pwd.h>
#include "config.h"
#define LF 10
#define CR 13
void *malloc();
void *realloc();
char *tildeexpand();
void getword();
char *makeword();
char *fmakeword();
char x2c();
void unescape_url();
void plustospace();
int rind();
int getline();
void send_fd();
char *tildeexpand(filename)
char *filename;
{
char *expanded, *orig, *tmp, save;
struct passwd *ppass;
filename++;
for (tmp=filename; *tmp != '\0' && *tmp != '/'; tmp++) ;
save = *tmp;
*tmp = '\0';
ppass = getpwnam(filename);
*tmp = save;
if (ppass == NULL)
return orig;
expanded = (char *) malloc(MAXPATHLEN + 1);
#ifdef USERDIR
if (USERDIR && strlen(USERDIR)>0)
sprintf(expanded, "%s/%s%s", ppass->pw_dir, USERDIR, tmp);
else
sprintf(expanded, "%s%s", ppass->pw_dir, tmp);
return expanded;
#endif
}
void getword(word,line, stop)
char *word, *line, stop;
{
int x = 0,y;
for(x=0;((line[x]) && (line[x] != stop));x++)
word[x] = line[x];
word[x] = '\0';
if(line[x]) ++x;
y=0;
while(line[y++] = line[x++]);
}
char *makeword(line, stop)
char *line, stop;
{
int x = 0,y;
char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));
for(x=0;((line[x]) && (line[x] != stop));x++)
word[x] = line[x];
word[x] = '\0';
if(line[x]) ++x;
y=0;
while(line[y++] = line[x++]);
return word;
}
char *fmakeword(f, stop, cl)
FILE *f;
char stop;
int *cl;
{
int wsize;
char *word;
int ll;
wsize = 102400;
ll=0;
word = (char *) malloc(sizeof(char) * (wsize + 1));
while(1) {
word[ll] = (char)fgetc(f);
if(ll==wsize) {
word[ll+1] = '\0';
wsize+=102400;
word = (char *) realloc(word,sizeof(char)*(wsize+1));
}
--(*cl);
if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
if(word[ll] != stop) ll++;
word[ll] = '\0';
return word;
}
++ll;
}
}
char x2c(what)
char *what;
{
register char digit;
digit = (what[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
digit *= 16;
digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
return(digit);
}
void unescape_url(url)
char *url;
{
register int x,y;
for(x=0,y=0;url[y];++x,++y) {
if((url[x] = url[y]) == '%') {
url[x] = x2c(&url[y+1]);
y+=2;
}
}
url[x] = '\0';
}
void plustospace(str)
char *str;
{
register int x;
for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
}
int rind(s,c)
char *s, c;
{
register int x;
for(x=strlen(s) - 1;x != -1; x--)
if(s[x] == c) return x;
return -1;
}
int getline(s, n, f)
char *s;
int n;
FILE *f;
{
register int i=0;
while(1) {
s[i] = (char)fgetc(f);
if(s[i] == CR)
s[i] = fgetc(f);
if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) {
s[i] = '\0';
return (feof(f) ? 1 : 0);
}
++i;
}
}
void send_fd(f, fd)
FILE *f, *fd;
{
int num_chars=0;
char c;
while (1) {
c = fgetc(f);
if(feof(f))
return;
fputc(c,fd);
}
}